﻿#include "GUISpectraModalDensityDialog.h"
#include "ui_GUISpectraDialogBase.h"

#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsSpectraManager.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKSpectraModalDensity.h"

namespace GUI
{
    GUISpectraModalDensityDialog::GUISpectraModalDensityDialog(int spectraDataID, bool isCreate) :
        GUISpectraDialogBase(spectraDataID, isCreate)
    {
        init();
    }

    GUISpectraModalDensityDialog::~GUISpectraModalDensityDialog()
    {

    }

    void GUISpectraModalDensityDialog::init()
    {
        Acoustics::FITKSpectraModalDensity* spectra = new Acoustics::FITKSpectraModalDensity();
        //设置图标
        _ui->label_image->setPixmap(QPixmap(":/browserIcons/modalDensity.png"));
        //初始化界面默认数据
        _ui->radioButton_RMS->setText(tr("Band Avg."));
        _ui->radioButton_PSD1->setText(tr("PSD"));
        _ui->radioButton_PSD2->setText(tr("PSD in g^2/Hz"));
        _ui->radioButton_RMS->setEnabled(false);
        _ui->radioButton_PSD1->setEnabled(false);
        _ui->radioButton_PSD2->setEnabled(false);
        _uiCurrentZeroDBValue = spectra->getDBDefaultCoefficient();
        _ui->lineEdit_Default->setText(QString::number(_uiCurrentZeroDBValue));
        _ui->lineEdit_UserDefined->setText(QString::number(_uiCurrentZeroDBValue));
        _ui->radioButton_RMS->setChecked(true);
        _ui->radioButton_FrequencyLog->setChecked(true);
        // 设置曲线画布标题
        setPlotTitle(tr("Modal Density"));
        //设置单位
        _RSMUnit = tr("modes/(rad/s)");

        //创建界面
        if (_isCreate) {
            spectra->init();
            _spectraUiDataID = spectra->getDataObjectID();
            QString name = "";
            if (_spectraManager) name = _spectraManager->checkName("New Modal Density Spectrum");
            _ui->lineEdit_Name->setText(name);
            //初始化数据
            updateHzValue(spectra->getSpectraValue());
            //更新form类型
            upateFormType();
            //更新曲线Y轴标签
            updatePlotYTitle();
            // 设置窗口标题
            setWindowTitle(tr("Create Modal Density Spectrum"));
        }
        else {
            //初始化数据
            Acoustics::FITKAcousticsAbstractSpectra* currentSpectra = getCurrentSpectraData();
            if (currentSpectra) {
                _spectraUiDataID = spectra->getDataObjectID();
                setDataToWidget(currentSpectra);
                //初始化界面默认数据
                _ui->lineEdit_Name->setText(spectra->getDataObjectName());
            }
            // 设置窗口标题
            setWindowTitle(tr("Edit Modal Density Spectrum"));
        }
        //更新单位
        updateZeroDBUnit();
        //初始化信号槽连接
        initConnect();
    }

    void GUISpectraModalDensityDialog::upateFormType()
    {
        if (getCurrentSpectraUiData()) {
            QString domainType = "";
            switch (getCurrentSpectraUiData()->getDomainType()){
            case Acoustics::FITKGlobalFrequencyDomain::DTNone:domainType = ""; break;
            case Acoustics::FITKGlobalFrequencyDomain::DTStandardOctave:domainType = tr("Band-averaged spectrum"); break;
            case Acoustics::FITKGlobalFrequencyDomain::DTStandard1Of3rdOctave:domainType = tr("Band-averaged spectrum"); break;
            case Acoustics::FITKGlobalFrequencyDomain::DTExact1OfnthOctave:domainType = tr("Band-averaged spectrum"); break;
            case Acoustics::FITKGlobalFrequencyDomain::DTConstantBandWidth:domainType = tr("Band-averaged spectrum"); break;
            }
            _ui->lineEdit_Form->setText(domainType);
        }
    }

    void GUISpectraModalDensityDialog::updatePlotYTitle()
    {
        QString title = "";
        if (_ui->checkBox_dB->isChecked()) {
            if (_ui->checkBox_AWeighting->isChecked()) {
                title = tr("dBA(ref = %1 modes/(rad/s))").arg(_uiCurrentZeroDBValue);
            }
            else {
                title = tr("dB(ref = %1 modes/(rad/s))").arg(_uiCurrentZeroDBValue);
            }
        }
        else
        {
            title = tr("modes/(rad/s)");
        }
        this->setPlotYTitle(title);
    }

    void GUISpectraModalDensityDialog::updateZeroDBUnit()
    {
        QString unit = tr("s");
        _ui->label_DefaultUnit->setText(unit);
        _ui->label_UserUnit->setText(unit);
        updateConstantAndRealValueUnit(unit);
    }

    Acoustics::FITKAcousticsAbstractSpectra * GUISpectraModalDensityDialog::createNewSpectra()
    {
        return new Acoustics::FITKSpectraModalDensity();
    }

    void GUISpectraModalDensityDialog::setDataToWidget(Acoustics::FITKAcousticsAbstractSpectra * data)
    {
        GUISpectraDialogBase::setDataToWidget(data);
        auto spectraData = dynamic_cast<Acoustics::FITKSpectraModalDensity*>(data);
        if (spectraData == nullptr)return;

        //DB
        if (spectraData->getDBEnableState() == true) {
            _ui->widget_LogAndLin->setEnabled(false);
            _ui->radioButton_Linear->setChecked(true);
            _ui->checkBox_AWeighting->setEnabled(true);
            _ui->groupBox_ZeroDB->setEnabled(true);
            _ui->checkBox_dB->setChecked(true);
            if (spectraData->getAWeightingEnableState() == true) {
                _ui->checkBox_AWeighting->setChecked(true);
            }
            else {
                _ui->checkBox_AWeighting->setChecked(false);
            }
        }
        else {
            _ui->widget_LogAndLin->setEnabled(true);
            _ui->checkBox_AWeighting->setEnabled(false);
            _ui->groupBox_ZeroDB->setEnabled(false);
            _ui->checkBox_dB->setChecked(false);
            _ui->checkBox_AWeighting->setChecked(false);
        }

        //零DB
        updateZeroDBWidget(spectraData->getDBUserCoefficient());
        _ui->lineEdit_Default->setText(QString::number(spectraData->getDBDefaultCoefficient()));
        if (spectraData->isDBUserDefined() == true) {
            _uiCurrentZeroDBValue = spectraData->getDBUserCoefficient();
        }
        else {
            _uiCurrentZeroDBValue = spectraData->getDBDefaultCoefficient();
        }
        _ui->lineEdit_UserDefined->setText(QString::number(_uiCurrentZeroDBValue));

        //更新曲线Y轴标题
        updatePlotYTitle();
        //更新form类型
        upateFormType();
        //计算并刷新界面频率值
        frontCalculate();
    }

    void GUISpectraModalDensityDialog::getDataFromWidget(Acoustics::FITKAcousticsAbstractSpectra* data)
    {
        GUISpectraDialogBase::getDataFromWidget(data);
        auto spectraData = dynamic_cast<Acoustics::FITKSpectraModalDensity*>(data);
        if (spectraData == nullptr)return;

        //存储DB
        if (_ui->checkBox_dB->isChecked()) {
            spectraData->setDBEnableState(true);
            if (_ui->checkBox_AWeighting->isChecked()) {
                spectraData->setAWeightingEnableState(true);
            }
        }
        else {
            spectraData->setDBEnableState(false);
            spectraData->setAWeightingEnableState(false);
        }

        //存储零DB
        const double EPS = 0.0000001;
        if ((_uiCurrentZeroDBValue - 0.0 <= EPS) || (!(_uiCurrentZeroDBValue > 1.0 || _uiCurrentZeroDBValue < 1.0))) {
            spectraData->setDBUserCoefficient(-1);
        }
        else {
            spectraData->setDBUserCoefficient(_uiCurrentZeroDBValue);
        }
    }
}
